﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace FreeFlow.Administration
{
  /// <summary>
  /// A version of a published WF workflow
  /// </summary>
  public class WorkflowVersion : DatabaseAware, IDeletable
  {
    internal WorkflowVersion(Server server, IDataReader reader) : base(server)
    {
      publishedTime = (DateTime)reader["eLoadedTime"];
      version = reader["eFileVersion"].ToString();
      fullyQualifiedTypeName = reader["eFullyQualifiedTypeName"].ToString();
      fullyQualifiedAssemblyName = reader["eFullyQualifiedAssemblyName"].ToString();
      guid = new Guid(reader["eWorkflowGuid"].ToString());
    }

    private string version;
    /// <summary>
    /// Gets the version number.
    /// </summary>
    public string Version
    {
      get { return version; }
    }

    private DateTime publishedTime;
    /// <summary>
    /// Gets the date/time that the workflow was published.
    /// </summary>
    public DateTime PublishedTime
    {
      get { return publishedTime; }
    }

    private string fullyQualifiedTypeName;
    /// <summary>
    /// Gets the fully qualified name of the workflow type.
    /// </summary>
    public string FullyQualifiedTypeName
    {
      get { return fullyQualifiedTypeName; }
    }

    private string fullyQualifiedAssemblyName;
    /// <summary>
    /// Gets the fully qualified name of the assembly that contains the workflow.
    /// </summary>
    public string FullyQualifiedAssemblyName
    {
      get { return fullyQualifiedAssemblyName; }
    }

    private Guid guid;
    /// <summary>
    /// Gets the GUID that uniquely identifies the workflow definition.
    /// </summary>
    public Guid Guid
    {
      get { return guid; }
    }

    #region IDeletable Members

    /// <summary>
    /// Deletes this version of the workflow from the database.
    /// </summary>
    public void Delete()
    {
      Server.ExecuteNonQuery(string.Format("DELETE FROM eMSWorkflowDefinition WHERE eWorkflowGuid = '{0}'", guid));
    }

    #endregion
  }
}
